#include"ProblemA.h"
#include"NewtonPoly.h"
#include<fstream>
#include<string>
#include<cmath>

/*C题中给定函数*/
double fC(double x){
    return 1 / (1 + 25 * x * x);
}

/**
 * @brief 将区间[-1,1]给200等分,取样本点,导出它们以及插值多项式在对应样本点处的值
 * 
 * 每次输出201组坐标,导出到`ProblemC_data/file?.txt`中(?=1,2,3,4).
 */
void print_nth_poly(double(*f)(double), int n){
    vector<double> inter_pts(n);
    for(int i = 0; i < n; i++)
        inter_pts[i] = cos((2.0*i+1.0) / (2.0*n) * M_PI);   //赋插值点

    string filename = "ProblemC_data/file" + to_string(n/5) + ".txt";//创建文件名  
    ofstream outputFile(filename);  //创建文件流对象
    if(outputFile.is_open()){
        for(int i = 0; i <= 200; i++){
            double x = -1.0 + i / 100.0;//取样本点
            double y = poly_at_x(f, inter_pts, x);  //取插值多项式在该样本点处的值
            outputFile << x << " " << y << endl;
        }
        outputFile.close(); //关闭文件  
        cout << "File " << filename << " created and written successfully.\n"; 
    }
    else{  
        cout << "Error creating file: " << filename << endl;  
    }
}

int main(){
    for(int n = 5; n <= 20; n += 5)
        print_nth_poly(&fC, n);
    return 0;
}